package com.gofunbuy.app.book;

import java.io.InputStream;
import java.util.HashMap;
import com.gofunbuy.R;
import com.gofunbuy.http.HttpClientImpl;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.DateUtils;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class TabBookActivityOld extends Activity implements CalendarView.OnCellTouchListener{
	//http://www.eye4gift.com/gofun/app_orderseat.php?user_id=0
	public static final String MIME_TYPE = "vnd.android.cursor.dir/vnd.exina.android.calendar.date";
	CalendarView mView = null;
	Handler mHandler = new Handler();
	TextView mTitle;
	TextView mName,mTelCode,mTel,mNum, mDate,mTime;
	Button mCommit;
	private String userId;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.book_layout);
		mTitle = (TextView) findViewById(R.id.topbar_title);
		mTitle.setText(R.string.title_book);
		
		userId = getIntent().getStringExtra("userid");
		 
	      
	}
	private void init(){
		
		mView = (CalendarView)findViewById(R.id.book_calendar);
	    mView.setOnCellTouchListener(this);
	     
	    mName = (TextView)findViewById(R.id.book_form_name);
	    mTelCode = (TextView)findViewById(R.id.book_form_tel_code);
	    mTel = (TextView)findViewById(R.id.book_form_tel);
	    mNum = (TextView)findViewById(R.id.book_form_numofpeople);
	    mDate = (TextView)findViewById(R.id.book_form_date);
	    mTime = (TextView)findViewById(R.id.book_form_time);
	    
		
		mCommit = (Button)findViewById(R.id.book_form_commit);
		mCommit.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				commit();
							
			}			
		});
		
		TextView mBack = (TextView)findViewById(R.id.topbar_back);
		mBack.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
			
		});
	}
	
	private void getUserInfo(){
		String uri = "http://www.eye4gift.com/gofun/app_get_userinfo_by_id.php";
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("shopcode", "goinside");
		params.put("userid", userId);
		InputStream is = HttpClientImpl.HttpPostData2(uri, params);
		if(is!=null){
			
		}
	}
	private void commit(){
		
		String name = mName.getText().toString().trim();
		String telCode = mTelCode.getText().toString().trim();
		String tel = mTel.getText().toString().trim();
		String num = mNum.getText().toString().trim();
		String date = mDate.getText().toString().trim();
		String time = mTime.getText().toString().trim();
		
		if(name.equals("")){
			Toast.makeText(TabBookActivityOld.this, "姓名不能为空！", 2500).show();
			return;
		}else if(telCode.equals("")){
			Toast.makeText(TabBookActivityOld.this, "区号不能为空！", 2500).show();
			return;
		}else if(tel.equals("")){
			Toast.makeText(TabBookActivityOld.this, "电话不能为空！", 2500).show();
			return;
		}else if(num.equals("")){
			Toast.makeText(TabBookActivityOld.this, "人数不能为空！", 2500).show();
			return;
		}else if(date.equals("")){
			Toast.makeText(TabBookActivityOld.this, "日期不能为空！", 2500).show();
			return;
		}else if(time.equals("")){
			Toast.makeText(TabBookActivityOld.this, "时间不能为空！", 2500).show();
			return;
		}
		
		String uri = "http://www.eye4gift.com/gofun/app_book_seat.php";
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("shopcode", "goinside");
		params.put("userid", userId);
		params.put("name", name);
		params.put("areacode", telCode);
		params.put("num", num);
		params.put("date", date);
		params.put("time", time);
						
		InputStream is = HttpClientImpl.HttpPostData2(uri, params);
		if(is!=null){
			String result = HttpClientImpl.convertStreamToString(is);
		}else{
			
		}	
	}
	public void onTouch(Cell cell) {
		//mView.mSelect = cell;
		mView.invalidate();
		
		int year  = mView.getYear();
		int month = mView.getMonth();
		int day   = cell.getDayOfMonth();
		
		// FIX issue 6: make some correction on month and year
		if(cell instanceof CalendarView.GrayCell) {
			// oops, not pick current month...				
			if (day < 15) {
				// pick one beginning day? then a next month day
				if(month==11)
				{
					month = 0;
					year++;
				} else {
					month++;
				}
				
			} else {
				// otherwise, previous month
				if(month==0) {
					month = 11;
					year--;
				} else {
					month--;
				}
			}
		}
		mDate.setText(day+"-"+month+"-"+year);
					
		int day2 = cell.getDayOfMonth();
		if(mView.firstDay(day2))
			mView.previousMonth();
		else if(mView.lastDay(day2))
			mView.nextMonth();
		else
			return;

		mHandler.post(new Runnable() {
			public void run() {
				Toast.makeText(TabBookActivityOld.this, DateUtils.getMonthString(mView.getMonth(), DateUtils.LENGTH_LONG) + " "+mView.getYear(), Toast.LENGTH_SHORT).show();
			}
		});
	}
}